﻿using RXY_Mall.Application.BankCard;
using RXY_Mall.Application.BankCard.Dto;
using RXY_Mall.Application.Finance;
using RXY_Mall.Application.Order;
using RXY_Mall.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RXY_Mall.Shop.Controllers
{
    public class FinanceController : BaseController
    {
        private IBankCardService _bankCardService;
        private IFinanceService _financeService;
        private IOrderService _orderService;
        public FinanceController()
        {
            _bankCardService = new BankCardService();
            _financeService= new FinanceService();
            _orderService = new OrderService();
        }

        public ActionResult Index()
        {
            var model= _financeService.GetShopBalances(ShopId);
            ViewBag.Settlementing = _financeService.GetSettlementing(ShopId);
            ViewBag.Withdrawal = _financeService.GetWithdrawal(ShopId);
            return View(model);
        }
        public ActionResult FinanceList()
        {
            return View();
        }

        public JsonResult FinanceLogList(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax,int status=-1, int type = -1)
        {
            int totalCount = 0;
            var list = _financeService.GetFinanceLogByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, status,type, ShopId, out totalCount);
            return Json(new { iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult FinanceReturn(int id=1)
        {
            var model = _financeService.GetShopBalances(ShopId);
            ViewBag.type = id;
            ViewBag.TypeName = id == 2 ? "欠款" : "冻结";
            return View(model);
        }
        public JsonResult FinanceReturnList(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, int type)
        {
            int totalCount = 0;
            var list = _financeService.GetFinanceReturnByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, type, ShopId, out totalCount);
            return Json(new { iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult FinanceReturnDetails(int id = 0)
        {
            if (id > 0)
            {
                var model = _financeService.GetFinanceReturnById(id);
                if (model != null && model.shop_id != ShopId)
                {
                    model = null;
                }
                return View(model);
            }
            else
            {
                return View();
            }
        }
        #region 提现银行卡
        public ActionResult BankCard()
        {
            ViewBag.id = ShopId;
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> AddorEditBankCard(BankCardInput input)
        {
            var result = await _bankCardService.AddorEditBankCard(MemberId, ShopId, input);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", id = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        [HttpGet]
        public JsonResult GetBankCard()
        {
            var model = _bankCardService.GetBankCardInfo(ShopId);
            if (model==null)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
            return Json(model, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 商家提现
        public ActionResult Withdrawal()
        {
            ViewBag.Balance = 0;
            var balance = _financeService.GetShopBalances(ShopId);
            if (balance!=null)
            {
                ViewBag.Balance = (balance.balances-balance.balance_due)>0? balance.balances - balance.balance_due:0;
            }
            ViewBag.Withdrawal = _financeService.GetWithdrawal(ShopId);
            ViewBag.Withdrawaling = _financeService.GetWithdrawaling(ShopId);
            return View();
        }

        public JsonResult Withdrawallist(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax,string code, int status)
        {
            int totalCount = 0;
            var userList = _financeService.GetWithdrawalByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, code, status, ShopId, out totalCount);
            return Json(new { iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = userList }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult WithdrawalDetails(int id=0)
        {
            if (id>0)
            {
                var model = _financeService.GetWithdrawalById(id);
                return View(model);
            }
            else
            {
                return View();
            }
        }

        public ActionResult AddWithdrawal()
        {
            ViewBag.Balance = 0;
            var balance = _financeService.GetShopBalances(ShopId);
            if (balance != null)
            {
                ViewBag.Balance = (balance.balances - balance.balance_due) > 0 ? balance.balances - balance.balance_due : 0;
            }
            ViewBag.Rate = _financeService.GetParam(2);
            ViewBag.BankCard = "请添加结算银行卡号！";
            var model = _bankCardService.GetBankCardInfo(ShopId);
            if (model != null)
            {
                ViewBag.BankCard = model.bank_name + " - " + model.bank_cardno;
            }
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> ApplyWithdraw(decimal money, string password)
        {
            //if (!Validator.IsDecimal(money))
            //{
            //    return Json(new { status = "n", info = "提现金额输入格式有误" });
            //}
            var result = await _financeService.ApplyWithdraw(MemberId, ShopId, money, password);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", id = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        } 
        #endregion

        public ActionResult FinanceCheck()
        {
            ViewBag.Settlementing = _financeService.GetSettlementing(ShopId);
            var balances = _financeService.GetShopBalances(ShopId);
            return View(balances);
        }
        public JsonResult FinanceCheckList(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, int status, string orderNo)
        {
            int totalCount = 0;
            var list = _financeService.GetSettlementByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, status, orderNo, ShopId, out totalCount);
            return Json(new { iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SettlementDetails(int id = 0)
        {
            if (id > 0)
            {
                var model = _financeService.GetSettlementById(id);
                if (model!=null&&model.shop_id!=ShopId)
                {
                    model = null;
                }
                return View(model);
            }
            else
            {
                return View();
            }
        }
        public ActionResult FinanceCheckDetails(int id = 0)
        {
            if (id > 0)
            {
                var model = _financeService.GetSettlementById(id);
                if (model != null && model.shop_id != ShopId)
                {
                    model = null;
                }
                return View(model);
            }
            else
            {
                return View();
            }
        }

        [HttpGet]
        public async Task<JsonResult> GoodsOrder(int id)
        {
            var result = await _orderService.GetOrder(id, ShopId);
            if (result.code == 1)
            {
                return Json(result.data, JsonRequestBehavior.AllowGet);
            }
            return null;
        }
    }
}